package ogmios.dialogs;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import ogmios.core.*;

/**
 * A dialog for letting the user build out a list that is returned as a String[]. 
 */
public class ListDialog extends JDialog
{
  private JButton add, addAtIndex, ok, cancel;
  private boolean accept = true;
  private GridLayout layout;
  private JScrollPane jsp;
  private JPanel itemPanel;
  private ActionListener removeAction;
  public static final String[] emptyStringArray = new String[0];

  public static String[] showDialog(String[] args, String title, String text)
  {
    ListDialog ld = new ListDialog(args, title, text);
    ld.setModal(true);
    ld.setVisible(true);
    return ld.accept ? ld.getArray() : emptyStringArray;
  }
  private String[] getArray()
  {
    String[] arr = new String[itemPanel.getComponentCount()/2];
    for (int  i = 0; i < itemPanel.getComponentCount(); i += 2)
    {
      JTextField field = (JTextField)itemPanel.getComponent(i);
      arr[i/2] = field.getText();
    }
    return arr;
  }
  private ListDialog(String[] args, String title, String text)
  {
    setTitle(title);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    itemPanel = new JPanel();
    itemPanel.setLayout(layout = new GridLayout(args.length, 2));
    removeAction =
      new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {

        int i = OgmiosUtilities.getComponentIndex(itemPanel, (Component)e.getSource());
        JTextField field = (JTextField)itemPanel.getComponent(i - 1);
        if (JOptionPane.showConfirmDialog(null, "Do you really want to remove:\n" + field.getText(), "Remove", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
        {
          itemPanel.remove(i);
          itemPanel.remove(i - 1);
          layout.setRows(layout.getRows() - 1);
          itemPanel.setSize(itemPanel.getPreferredSize());
        }
      }
    };

    for (String s : args)
    {

      JButton remove = new JButton("Remove");
      remove.addActionListener(removeAction);
      itemPanel.add(new JTextField(s));
      itemPanel.add(remove);
    }
    add = new JButton("Add");
    add.addActionListener(
      new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        String s = JOptionPane.showInputDialog(null, "What is the item?");
        if (s != null)
        {
          JButton remove = new JButton("Remove");
          remove.addActionListener(removeAction);
          itemPanel.add(new JTextField(s));
          itemPanel.add(remove);
          layout.setRows(layout.getRows() + 1);
          itemPanel.setSize(itemPanel.getPreferredSize());
        }
      }
    }
    );
    addAtIndex = new JButton("Add At Index");
    addAtIndex.addActionListener(
      new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        String s = JOptionPane.showInputDialog(null, "What is the item?");
        if (s != null)
        {
          int max;
          String n = JOptionPane.showInputDialog(null, "What index is " + s + " to be placed?\nPotential indices range from 1 to " + (max = (itemPanel.getComponentCount() / 2 + 1)));
          if (n != null)
          {
            int i = OgmiosUtilities.tryParse(n);
            if (OgmiosUtilities.success && i > 0 && i <= max )
            {
              i = (i - 1) * 2;
              JButton remove = new JButton("Remove");
              remove.addActionListener(removeAction);
              itemPanel.add(new JTextField(s), i);
              itemPanel.add(remove, i + 1);
              layout.setRows(layout.getRows() + 1);
              itemPanel.setSize(itemPanel.getPreferredSize());
            }
          }
        }
      }
    }
    );
    ok = new JButton("Ok");
    ok.addActionListener(
      new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        accept = true;
        setVisible(false);
      }
    }
    );
    cancel = new JButton("Cancel");
    cancel.addActionListener(
      new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        setVisible(false);
      }
    }
    );

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BorderLayout());
    buttonPanel.add(add, BorderLayout.WEST);
    buttonPanel.add(addAtIndex, BorderLayout.EAST);
    JPanel decisionPanel = new JPanel();
    decisionPanel.setLayout(new GridLayout(1, 2));
    decisionPanel.add(ok);
    decisionPanel.add(cancel);
    buttonPanel.add(decisionPanel, BorderLayout.SOUTH);

    getContentPane().add(jsp = new JScrollPane(itemPanel));
    JLabel label;
    getContentPane().add(label = new JLabel(text), BorderLayout.NORTH);
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    pack();

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setSize((int)(screenSize.width / 3), (int)(screenSize.height / 2));

//setSize(350, 350);
  }
}
